package cn.iocoder.yudao.module.dine.print.service.core;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.framework.business.basic.service.MerchantExtensionService;
import cn.iocoder.yudao.framework.business.basic.service.impl.AbstractService;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.module.api.dine.enums.DinePrintSupplyTypeEnum;
import cn.iocoder.yudao.module.dine.print.convert.DinePrintConfigConvert;
import cn.iocoder.yudao.module.dine.print.dao.DinePrintConfigMapper;
import cn.iocoder.yudao.module.dine.print.pojo.po.DinePrintConfig;
import cn.iocoder.yudao.module.dine.print.pojo.vo.DinePrintConfigVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * 服务
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/08/22
 */
@Service
public class CoreDinePrintConfigService extends AbstractService<DinePrintConfig, DinePrintConfigVO> implements MerchantExtensionService<DinePrintConfig> {
    @Resource
    private DinePrintConfigMapper dinePrintConfigMapper;

    @Override
    public DinePrintConfigMapper getInjectedMapper() {
        return dinePrintConfigMapper;
    }

    @Override
    public DinePrintConfigVO convertToViewObject(DinePrintConfig entity) {
        return DinePrintConfigConvert.INSTANCE.toViewObject(entity);
    }

    @DataPermission(enable = false)
    @Override
    public DinePrintConfig getOrCreate(Long shopId) {
        CoreLambdaQueryWrapper<DinePrintConfig> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.eq(DinePrintConfig::getShopId, shopId);
        wrapper.orderByDesc(DinePrintConfig::getId);
        wrapper.limitN(1);
        DinePrintConfig entity = dinePrintConfigMapper.selectOne(wrapper);
        if (entity == null) {
            entity = new DinePrintConfig();
            entity.setShopId(shopId);
            entity.setSupplyType(DinePrintSupplyTypeEnum.FeiE.getValue());
            entity.setPrinterName("");
            entity.setPrinterSn("");
            entity.setPrinterKey("");
            dinePrintConfigMapper.insert(entity);
        }
        return entity;
    }

    public List<DinePrintConfig> getList(Set<Long> shopIds) {
        if (CollUtil.isEmpty(shopIds)) {
            return List.of();
        }
        CoreLambdaQueryWrapper<DinePrintConfig> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.in(DinePrintConfig::getShopId, shopIds);
        return dinePrintConfigMapper.selectList(wrapper);
    }

}
